//
//  LAFEngine.m
//  HuLaQuan
//
//  Created by liyan on 16/1/29.
//  Copyright © 2016年 yuwubao. All rights reserved.
//

#import "LAFEngine.h"

#import "UIImage+YuExt.h"


@implementation LAFEngine
//提交
-(void)lostandFound:(NSString *)ownerName andOwnerPhone:(NSString *)phone andOwnerLoatTime:(NSString *)lostTime andOwnerDesc:(NSString *)desc andOwnerPic:(NSArray *)picURLAry
{
    NSString *src = [NSString stringWithFormat: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"LostandFound\" User=\"%@\"><node><ownerName>%@</ownerName><ownerPhone>%@</ownerPhone><lostTime>%@</lostTime><desc>%@</desc><lostPic>%@</lostPic><attach></attach></node></msg>",[CurrentUser sharedInstance].ID,ownerName,phone,lostTime,desc,[picURLAry firstObject]];
    //
    NSLog(@"%@ -- src ---",src);
    
    //use Yu
    [YuHTTPClient XMLFMDataWithUrl:FM_SERVER_HOST parameters:src success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            result = @{@"isSuccess":@YES};
        }else{
            result = @{@"isSuccess":@NO,@"message":item};
        }
        //
        [[NSNotificationCenter defaultCenter] postNotificationName:@"LostandFoundFinish" object:result];
    }];
}

//上传图片
-(void)imageUpload:(NSArray *)imageArray
{
    
    NSMutableString *srcString = [[NSMutableString alloc] initWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg service=\"ImageUpload\" User=\"%@\"><node>",[CurrentUser sharedInstance].ID];
    
    for (UIImage *image in imageArray) {
        NSString *base64ImageString = [image compressAndConvertToBase64];
        [srcString appendFormat:@"<imgContent>%@</imgContent>",base64ImageString ? base64ImageString : @""];
    }
    [srcString appendString:@"</node></msg>"];
    //
    NSLog(@"%@ -- src ---",srcString);
    
    //use Yu
    [YuHTTPClient XMLFMDataWithUrl:FM_SERVER_HOST parameters:srcString success:^(BOOL isSuccess, id item){
        
        NSDictionary *result;
        if (isSuccess) {
            result = @{@"isSuccess":@YES,@"result":item};
        }else{
            result = @{@"isSuccess":@NO,@"message":item};
        }
        //
        [[NSNotificationCenter defaultCenter] postNotificationName:@"LostImageUploadFinish" object:result];
    }];
}
@end
